
function init_carousel() {
	var carousel_urls = ['http://www.w3schools.com/','http://jquery.com/','http://www.php.net/'];
	var carousel_png = ['images/hero/hero1.png','images/hero/hero2.png','images/hero/hero3.png'];
	var car_pos = 0;
	var arraylen = carousel_png.length;
		
	$('#car_url').attr('href',carousel_urls[0]); // set default URL

	$('#meey_left').mouseover(function(){

		$(this).css('background-color','#000');
		$(this).css('background-image','url(images/www/arrow_left_dn.png)');
	});	
	
	$('#meey_left').mouseout(function(){

		$(this).css('background-color','#2a738f');
		$(this).css('background-image','url(images/www/arrow_left.png)');
	});	
	
	$('#meey_right').mouseover(function(){
	
		$(this).css('background-color','#000');
		$(this).css('background-image','url(images/www/arrow_right_dn.png)');
	});
	
	$('#meey_right').mouseout(function(){

		$(this).css('background-color','#2a738f');
		$(this).css('background-image','url(images/www/arrow_right.png)');
	});	
	
	
	$('#meey_left').click(function(e){
		e.preventDefault();
		previous();
	});	
	
	$('#meey_right').click(function(e){
		e.preventDefault();
		next();
	});
	


	function previous()
	{
	
		if(car_pos == '1') // going back after getting to the second image
		{
			car_pos = car_pos - 1;
			manageCounter(4, 3);
		}
		else if(car_pos > 0)
		{
			car_pos = car_pos - 1;
			manageCounter((arraylen + 2), (car_pos + 3));
		}
		else // if it's the first element
		{
			car_pos = (arraylen - 1);
			manageCounter(3, (car_pos + 3));
		}
		
		changeImg( car_pos );

	}
	
	function next()
	{
		if(car_pos != (arraylen - 1)) // if it's not the last element
		{
			car_pos = car_pos + 1;
			manageCounter((car_pos + 2), (car_pos + 3));
		}
		else // if it is the last element
		{
			car_pos = 0;
			manageCounter((arraylen + 2), (car_pos + 3));
		}
		
		changeImg( car_pos );
		scrollCarousel();
	}
	
	function changeImg( index ){
	
		var img = new Image();
			img.onload = function(){
				$('#meey_carousel').stop().fadeOut('slow', function(){
					$(this).css('background','url(' + img.src +') no-repeat');
				}).fadeIn(500);
		}
		img.src = carousel_png[index];
		
		$('#car_url').attr('href',carousel_urls[index]);	
    }
	
	function manageCounter(prevElem, currElem)
	{
		$('.meey_carousel_content').find('div:nth-child(' + prevElem + ')').removeClass('car_counter_hl');
		$('.meey_carousel_content').find('div:nth-child(' + prevElem + ')').addClass('car_counter');
		
		$('.meey_carousel_content').find('div:nth-child(' + currElem + ')').addClass('car_counter_hl');		
	}
	
	
	function scrollCarousel(){
		setTimeout(next, 5000);
	}
	// start when the page has loaded
	$(window).load(function() {
			scrollCarousel();
	});
	
}

$(document).ready(function() {
	init_carousel();
});